/*
Kiwi Editor is a framework for building WYSIWYG web editors.
Copyright (C) 2011  Roosevelt de Lima Sardinha

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Contact the author at roosevelt1@gmail.com (Roosevelt) 
*/

/**
 * A document where someone can add and set EOs. It represents all the content built in an edition area.
 * @author Roosevelt de Lima Sardinha
 * @param {Object} json Optional JSON to build the document.
 * @constructor
 */
function editorDocument(json){
	/**
	 * The unique id of the document.
	 * @type {Int}
	 */
    this.id = null;
	
	/**
	 * The name of the document.
	 * @type {String}
	 */
	this.name = null;
	
	/**
	 * The description of the content of the document.
	 * @type {String}
	 */
	this.description = null;
	
	/**
	 * The editionArea where this document is opened.
	 * @type {Object}
	 */
	this.editionArea = null;
	var myJSON = null;
	if (json != null){
		myJSON = json;
	}

	/**
	 * Opens the content of a document in a edition area.
	 * @param {Object} editionArea The edition area where the content is going to be added.
	 */
    this.open = function(editionArea){
		if (this.id != null) {
			var docJSON = this.generateJSONfromDB();
			myJSON = docJSON;
		}
		
		if (myJSON != null){
			editionArea.EOM.createEOsByJSON(myJSON);
		}
		
		this.editionArea = editionArea;
    }
    
	/**
	 * Saves the content of a document to the database.
	 */
    this.save = function(){
		var exists = null;
		var id;
    	var EOsJSON = this.editionArea.EOM.EO_JSON();
		var docJSON = {};
		docJSON.name = this.name;
		docJSON.description = this.description;
		docJSON.eos = EOsJSON;
		
		var strJSON = JSON.stringify(docJSON);
						
		var params = [];
		params['json'] = strJSON;
				
		var myRequest = new request();	
		if (this.id != null) {
			myRequest.sendSincRequest('GET', null, "documents/" + this.id + "/exist", function(response){
				exists = response;
			});
		}
		
		// Check if document already exists
		if ((exists != null) && JSON.parse(exists)){
			params['_method'] = 'PUT';
	        myRequest.sendSincRequest('POST', params, "documents/" + this.id, function(response){
			});
		}else{
	        myRequest.sendSincRequest('POST', params, "documents", function(response){
				id = response;
			});
			this.id = JSON.parse(id);		
		}	
    }
	
	/**
	 * Generates a JSON corresponding to the document.
	 * @return A JSON describing this document.
	 * @type {Object}
	 */
	this.generateJSON = function(){
		var EOsJSON = this.editionArea.EOM.EO_JSON();
		docJSON = {};
		docJSON.name = this.name;
		docJSON.description = this.description;
		docJSON.eos = EOsJSON;
		return docJSON;
	}
    
	/**
	 * Generates a JSON from the content in the database corresponding to the document. Maybe not what is on the screen in the edition area.
	 * To update these data you need to save the document
	 * @return A JSON describing the document.
	 * @type {Object}
	 */    
    this.generateJSONfromDB = function(){
    	var EOsStr;
        var myRequest = new request();
        myRequest.sendSincRequest('GET', null, "documents/" + this.id, function(response){
            EOsStr = response;
        });
		var EOsJSON = JSON.parse(EOsStr);
		return EOsJSON;
    }
    
	/**
	 * Generates a XML from the content of the document.
	 * @return The XML corresponding to the document.
	 * @type {String}
	 */
    this.generateXML = function(){
    	var EOsStr;
        var myRequest = new request();
        myRequest.sendSincRequest('GET', null, "documents/" + this.id  + ".xml", function(response){
            EOsStr = response;
        });
		return EOsStr;    
    }
    
	/**
	 * Generates a HTML from the content of the document.
	 * @return The HTML corresponding to the document.
	 * @type {String}
	 */
    this.generateHTML = function(){
		var ind = new indenter();
    	return ind.indentHTML(this.editionArea.EOM.getEOsHTML());
    }
    
	/**
	 * Changes a property of the document to some value
	 * @param {String} propertyName The name of the property to be changed.
	 * @param {Any} value The value to be set to the property.
	 */		
    this.setProperty = function(property, value){
    	switch(property){
			case 'name':
				this.name = value;
				break;
				
			case 'description':
				this.description = value;
				break;
				
			default:				
		}
    }
		
	/**
	 * Closes a document previously opened. Removes all EOs of the edition Area and sets this document edition area property to null.
	 */
	this.close = function(){
		this.editionArea.EOM.removeAllEOs();
		this.editionArea = null;
	}	
}
